package lt.egidijus.aatest.generator;

import java.util.ArrayList;
import java.util.List;

import lt.egidijus.aatest.generator.rules.Rule;
import lt.egidijus.aatest.model.AAtestClass;
import lt.egidijus.aatest.utils.Constants;
import lt.egidijus.aatest.utils.Utils;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

public class TestSuiteGenerator {
	
	protected ICompilationUnit generatedUnit;

	protected Rule defaultValueGenerator;

	protected List<Rule> valueGenerators = new ArrayList<Rule>(0);
	protected boolean poz = false;

	private void save(IPackageFragment pack, String name, String contents) {

		contents = Utils.formatCode(contents);
		if (contents != null) {
			ICompilationUnit cTest;
			try {
				cTest = pack.createCompilationUnit(name, contents, true, null);
				generatedUnit = cTest;
			} catch (JavaModelException e) {
				e.printStackTrace();
			}

		} 
	}



	private String createClassHeader(List<AAtestClass> selectedTestList) throws JavaModelException {
		StringBuilder b = new StringBuilder();
		b.append("package " + Constants.testSuitePackage + ";\n\n");
		b.append("import junit.framework.Test;\n");
		b.append("import junit.framework.TestSuite;\n\n");
		b.append("public class " + Constants.testSuiteClass + " extends TestSuite {\n\n");
		b.append("  public static Test suite() {\n\n");
		b.append("    TestSuite suite = new TestSuite();\n");
		for (AAtestClass cUnit : selectedTestList){
			String name = cUnit.getTestRandom().getAllTypes()[0].getFullyQualifiedName();
			b.append("    suite.addTestSuite("+name+".class);\n");
		}
		b.append("    return suite;\n");
		b.append("  }");
		b.append("}");
		return b.toString();
	}
	
	public void createTestSuite(List<AAtestClass> selectedTestList){
		try {
		IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
		IJavaProject project = JavaCore.create(iProject);
		IFolder f = project.getProject().getFolder(
				Constants.folderName);
		if (!f.exists()) {
			f.create(true, true, null);
		}
		IPackageFragmentRoot rootFolder = project
				.getPackageFragmentRoot(f);
		IPackageFragment fragment = rootFolder
				.createPackageFragment(Constants.testSuitePackage,
						true, null);
		

		save(fragment, Constants.testSuiteClass+".java", createClassHeader(selectedTestList));
		} catch (JavaModelException e) {
			e.printStackTrace();
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}



	public ICompilationUnit getGeneratedUnit() {
		return generatedUnit;
	}




}
